package com.example.aigc_education.controller;

import com.example.aigc_education.anno.RequestKeyParam;
import com.example.aigc_education.anno.RequestLock;
import com.example.aigc_education.common.BusinessException;
import com.example.aigc_education.domain.dto.TestInfoDTO;
import com.example.aigc_education.domain.request.*;
import com.example.aigc_education.domain.vo.*;
import com.example.aigc_education.service.PlanService;
import com.example.aigc_education.service.ThemeService;
import com.example.aigc_education.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Slf4j
@RestController
@RequestMapping("/theme")
public class ThemeController {
    @Resource
    private ThemeService themeService;

    @Resource
    private PlanService planService;

    @RequestLock(prefix = "addThemeV2:", expire = 3)
    @PostMapping("/add/v2")
    public ResultUtil addThemeV2(@RequestKeyParam @RequestBody @Validated AddThemeV2Request request){
        AddThemeRespVO addThemeResponseVO = themeService.addThemeV2(request);
        return ResultUtil.success("success",addThemeResponseVO);
    }

    @RequestLock(prefix = "addTheme:", expire = 3)
    @PostMapping("/add")
    public ResultUtil addTheme(@RequestKeyParam @RequestBody @Validated AddThemeRequest addThemeRequest){
        boolean add =  themeService.addTheme(addThemeRequest);
        if (!add) {
            throw new BusinessException("添加失败");
        }
        return ResultUtil.success("添加成功");
    }

    @RequestLock(prefix = "updateThemeTitle:", expire = 3)
    @PostMapping("/update/title")
    public ResultUtil updateThemeTitle(@RequestKeyParam @RequestBody @Validated UpdateThemeTitleRequest request){
        UpdateThemeTitleRespVO response = themeService.updateThemeTitle(request);
        return ResultUtil.success("success",response);
    }



    /**
     * 展示该计划下的主题树
     * @param planId 计划id
     * @return 该计划的主题树
     */
    @GetMapping("/{planId}/showThemeTree")
    public ResultUtil showThemeTree(@PathVariable @NotNull(message = "计划id不能为空") Long planId){
        ThemeTreeVO themeTree =  themeService.showThemeTree(planId);
        return ResultUtil.success(themeTree);
    }

    /**
     * 展示该计划下的所有主题
     * @param planId 计划id
     * @return 该计划的主题树
     */
    @GetMapping("/{planId}/getAllTheme")
    public ResultUtil getAllThemeForPlan(@PathVariable @NotNull(message = "计划id不能为空") Long planId){
        AllThemeVO themeTree =  themeService.getAllThemeForPlan(planId);
        return ResultUtil.success(themeTree);
    }



    /**
     * 修改主题状态
     * @param setThemeStatusRequest 主题id
     * @return 返回true
     */
    @RequestLock(prefix = "updateThemeStatus:", expire = 3)
    @PutMapping("/updateStatus")
    public ResultUtil updateThemeStatus(@RequestKeyParam @Validated SetThemeStatusRequest setThemeStatusRequest) {
        boolean removed = themeService.updateThemeStatus(setThemeStatusRequest);
        if (!removed) {
            throw new BusinessException("修改失败");
        }
        return ResultUtil.success("修改成功");
    }

    /**
     * 修改主题状态V2版
     * @param updateThemeStatusRequest 主题id
     * @return 返回true
     */
    @RequestLock(prefix = "updateThemeStatusV2:", expire = 3)
    @PutMapping("/updateStatus/v2")
    public ResultUtil updateThemeStatusV2(@RequestKeyParam @Validated UpdateThemeStatusRequest updateThemeStatusRequest) {
        boolean removed = themeService.updateThemeStatusV2(updateThemeStatusRequest);
        if (!removed) {
            throw new BusinessException("修改失败");
        }
        return ResultUtil.success("success",new IdVO(updateThemeStatusRequest.getThemeId()));
    }

    /**
     * 删除主题
     * @param themeId 主题id
     * @return 返回删除主题结果
     */
    @RequestLock(prefix = "deleteTheme:", expire = 3)
    @PutMapping("/{themeId}/delete")
    public ResultUtil deleteTheme(@RequestKeyParam @PathVariable @NotNull(message = "主题id不能为空") Long themeId) {
        boolean removed = themeService.deleteTheme(themeId);
        if (!removed) {
            throw new BusinessException("删除失败");
        }
        return ResultUtil.success("删除成功");
    }

    /**
     * 删除主题
     * @param themeId 主题id
     * @return 返回删除主题结果
     */
    @RequestLock(prefix = "deleteThemeV2:", expire = 3)
    @PostMapping("/{themeId}/delete/v2")
    public ResultUtil deleteThemeV2(@RequestKeyParam @PathVariable @NotNull(message = "主题id不能为空") Long themeId) {
        boolean removed = themeService.deleteThemeV2(themeId);
        if (!removed) {
            throw new BusinessException("删除失败");
        }
        return ResultUtil.success("success",new IdVO(themeId));
    }

    /**
     * 更新主题学习时间
     * 当天第一次进行主题学习，MongoDB插入文档，保存主题学习时间
     * 非当天第一次进行主题学习，只更新学习的结束时间，并且自动计算学习总时间
     *
     * @param themeId       主题id
     * @param status        判断时间状态
     * @return              操作成功信息
     */
    @RequestLock(prefix = "studyTime", expire = 3)
    @GetMapping("/studyTime")
    public ResultUtil studyTime(@RequestKeyParam @RequestParam("themeId") @NotNull(message = "主题id不能为空") Long themeId,
                                @RequestParam("status") @Size(max = 1, message = "更新时间状态异常") Integer status
    ){

        themeService.themeStudyTime(themeId, status);
        return ResultUtil.success("操作成功");
    }

    /**
     * 获取最近七天的主题学习时间和测验情况
     * @param themeId   主题id
     * @param planId    计划id
     * @return          VO
     */
    @RequestLock(prefix = "getThemeStudy", expire = 3)
    @GetMapping("/study/{themeId}")
    public ResultUtil getThemeStudy(@RequestKeyParam @PathVariable("themeId")
                                            @NotNull(message = "主题id不能为空") Long themeId,
                                    @RequestParam("planId") @NotNull(message = "计划id不能为空") Long planId

    ){
        try{

            ThemeStudyTimeVO themeStudyTimeVO = themeService.getStudyInfo(themeId);
            TestInfoDTO testInfo = planService.getTestInfo(planId);
            themeStudyTimeVO.setPlanId(planId);
            themeStudyTimeVO.setTestInfo(testInfo);

            return ResultUtil.success(themeStudyTimeVO);
        }catch (Exception e){
            throw new BusinessException("获取主题学习异常");
        }

    }

}
